package com.dingli.service.impl;

import com.dingli.framework.entity.Member;
import com.dingli.framework.entity.Role;
import com.dingli.framework.mapper.RoleMapper;
import com.dingli.framework.util.Response;
import com.dingli.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoleService implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Response<PageInfo<Role>> relist(String search, Integer pageNum, Integer pageSize) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Role> info = new PageInfo<>(roleMapper.relist(search));
        return new Response<>(200, "success", info);
    }

    @Override
    public Response<Long> reinsert(Role role) {
        int count = roleMapper.reinsert(role);
        if (count > 0) {
            return new Response(200, "success", role.getRoleId());
        } else {
            return new Response<>(500, "failed");
        }
    }

    @Override
    public Response<Role> reselectById(Long roleID) {
        return new Response(200, "success", roleMapper.reselectById(roleID));
    }

    @Override
    public Response<Object> reupdateById(Role role) {
        int count = roleMapper.reupdateById(role);
        if (count > 0) {
            return new Response<>(200, "success");
        } else {
            return new Response<>(500, "failed");
        }
    }

    @Override
    public Response<Object> redeleteById(Long roleID) {
        int count = roleMapper.redeleteById(roleID);
        if (count > 0) {
            return new Response<>(200, "success");
        } else {
            return new Response<>(500, "failed");
        }
    }
}
